package com.greentravel.view;

import com.greentravel.model.User;
import com.greentravel.service.UserService;
import com.greentravel.service.impl.UserServiceImpl;
import com.greentravel.util.GUIUtil;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 登录界面
 * 实现用户登录功能和注册入口
 */
public class LoginView extends JFrame {

    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton loginButton;
    private JButton registerButton;

    private UserService userService;

    /**
     * 构造方法，初始化登录界面
     */
    public LoginView() {
        userService = new UserServiceImpl();
        initComponents();
    }

    /**
     * 初始化界面组件
     */
    private void initComponents() {
        // 设置窗口基本属性
        setTitle("绿色出行倡导系统 - 登录");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        GUIUtil.centerComponent(this);

        // 设置UI样式
        GUIUtil.setUIStyle();

        // 创建面板和布局
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
        mainPanel.setBackground(Color.WHITE);

        // 标题面板
        JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        titlePanel.setBackground(Color.WHITE);
        JLabel titleLabel = GUIUtil.createLabel("绿色出行倡导与数据记录系统", GUIUtil.TITLE_FONT);
        titleLabel.setForeground(GUIUtil.PRIMARY_COLOR);
        titlePanel.add(titleLabel);

        // 输入面板
        JPanel inputPanel = new JPanel(new GridLayout(2, 2, 10, 10));
        inputPanel.setBackground(Color.WHITE);

        JLabel usernameLabel = GUIUtil.createLabel("用户名:", GUIUtil.NORMAL_FONT);
        usernameField = new JTextField();
        usernameField.setFont(GUIUtil.NORMAL_FONT);

        JLabel passwordLabel = GUIUtil.createLabel("密码:", GUIUtil.NORMAL_FONT);
        passwordField = new JPasswordField();

        inputPanel.add(usernameLabel);
        inputPanel.add(usernameField);
        inputPanel.add(passwordLabel);
        inputPanel.add(passwordField);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        buttonPanel.setBackground(Color.WHITE);

        loginButton = GUIUtil.createButton("登录");
        registerButton = GUIUtil.createButton("注册");

        buttonPanel.add(loginButton);
        buttonPanel.add(registerButton);

        // 将面板添加到主面板
        mainPanel.add(titlePanel, BorderLayout.NORTH);
        mainPanel.add(inputPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        // 添加主面板到窗口
        setContentPane(mainPanel);

        // 添加按钮事件
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                login();
            }
        });

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                openRegister();
            }
        });

        // 设置默认按钮
        getRootPane().setDefaultButton(loginButton);
    }

    /**
     * 处理登录业务逻辑
     */
    private void login() {
        String username = usernameField.getText().trim();
        String password = new String(passwordField.getPassword());

        // 校验输入
        if (username.isEmpty() || password.isEmpty()) {
            GUIUtil.showErrorMessage(this, "用户名和密码不能为空");
            return;
        }

        // 调用服务层进行登录验证
        User user = userService.login(username, password);

        if (user != null) {
            // 登录成功，打开主界面
            GUIUtil.showInfoMessage(this, "登录成功，欢迎 " + user.getName() + "!");
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    MainView mainView = new MainView(user);
                    mainView.setVisible(true);
                    dispose(); // 关闭登录窗口
                }
            });
        } else {
            // 登录失败
            GUIUtil.showErrorMessage(this, "用户名或密码错误，请重试");
            passwordField.setText("");
        }
    }

    /**
     * 打开注册界面
     */
    private void openRegister() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                RegisterView registerView = new RegisterView();
                registerView.setVisible(true);
                setVisible(false); // 隐藏登录窗口
            }
        });
    }

    /**
     * 主方法，程序入口点
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                LoginView loginView = new LoginView();
                loginView.setVisible(true);
            }
        });
    }
}